package com.dotflat.xj.repository;

import com.dotflat.xj.domain.TargetValueRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface TargetValueRecordRepository extends JpaRepository<TargetValueRecord, String>, JpaSpecificationExecutor<TargetValueRecord> {

    @Query(value="SELECT * FROM xj_target_value_record  where  areaId = ?1  and targetProjectValueId = ?2 and  DATE_FORMAT(created,'%Y-%m-%d') = DATE_FORMAT(?3,'%Y-%m-%d') ORDER BY created DESC LIMIT 1 ",nativeQuery = true)
    TargetValueRecord selectTargetValueRecord(String areaId,String targetProjectValueId,String created);


    @Query(value="SELECT * FROM xj_target_value_record  where  areaId = ?1  and targetId = ?2  and recordId = ?3 and  DATE_FORMAT(created,'%Y-%m-%d') = DATE_FORMAT(?4,'%Y-%m-%d')",nativeQuery = true)
    List<TargetValueRecord> getRecordIdList(String areaId,String targetId,String recordId,String created);

    @Transactional
    @Modifying
    @Query(value="delete from xj_target_value_record where recordId = ?1 and DATE_FORMAT(created,'%Y-%m-%d') = DATE_FORMAT(?2,'%Y-%m-%d') ",nativeQuery = true)
    int deleteAllByRecordIdTime(String recordId,String created);


    @Transactional
    @Modifying
    @Query(value="delete from xj_target_value_record where  targetProjectValueId = ?1  ",nativeQuery = true)
    int deleteTargetProjectValueId(String targetProjectValueId);
}
